package com.whq.mvc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.googlecode.aviator.AviatorEvaluator;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Test01 {
    public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
        List<String> list = Arrays.asList("");
        for (String str : list) {

        }
    }

    private static String toMin(int second) {
        return BigDecimal.valueOf(second).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_HALF_DOWN).stripTrailingZeros().toPlainString();
    }

    public static void test01() throws UnsupportedEncodingException {
        String str = "{{自定义_保证金金额_租赁押金+保证金金额_水电押金+首期金额_[租金]_显示金额大写}}";
        String rKey = StringUtils.strip(str, "{}#");
        str = StringUtils.substring(rKey, rKey.indexOf("_") + 1, rKey.lastIndexOf("_"));    // 表达式
        String[] keys = StringUtils.split(str.replaceAll(" ", ""), "\\+|\\-|\\*\\/|\\(|\\)|\\&");
        str = str.replace("首期金额_[租金]", "abc");
        System.out.println(str);

    }

    public synchronized void generCode() throws InterruptedException {
        StringBuilder sb = new StringBuilder("YH");
        sb.append(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN));
        TimeUnit.MILLISECONDS.sleep(10);
        System.out.println(sb.toString());
    }

    public static void test011() throws UnsupportedEncodingException {
        String password = "123456a";
        String expression = "(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
        System.out.println(password.matches(expression));
    }

    public static void test02() {
        String exp = "1+(2)(3)";
        Map<String, Object> env = new HashMap<String, Object>();
        env.put("p1", new BigDecimal("3"));
        env.put("p2", BigDecimal.ZERO);
        //BigDecimal s = BigDecimal.valueOf((Long) AviatorEvaluator.execute(exp,env));
        Long s = (Long) AviatorEvaluator.execute(exp, env);
        //String s1 = s.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        System.out.println(s.toString());
    }

    public static void test03() {
        String ext = "{{自定义:(累计应收:管理费、维修费,商场停车费)+(累计应收:管理费、维修费,商场停车费2)_显示金额大写}}";
        String rkey = StringUtils.strip(ext, "{}#");
        String[] contents = rkey.substring(rkey.indexOf(":") + 1).split("\\_");
        String sss = contents[0];
        Map<String, Object> env = new HashMap<String, Object>();
        String[] keys = StringUtils.replaceAll(contents[0], " ", "").split("\\+|\\-|\\*\\/|\\(|\\)|\\&", -1);
        System.out.println(Arrays.toString(keys));
        for (int i = 0; i < keys.length; i++) {
            if (StringUtils.isEmpty(keys[i])) continue;
            String key = "p" + i;
            sss = sss.replaceFirst(keys[i], key);
            env.put(key, BigDecimal.valueOf(100));
        }
        System.out.println(sss);
        BigDecimal s = (BigDecimal) AviatorEvaluator.execute(sss, env);
        String s1 = s.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        System.out.println(s1);
    }
}
